package com.cat3studios.tile.graphics;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;

import java.util.LinkedList;

import com.cat3studios.tile.engine.Subsystem;

public class RenderManager implements Subsystem{
	LinkedList<Renderable	> renderablesList;
	static RenderManager instance;
	RenderManager(){
		System.out.print("Initilizing RenderManager.\n");
		renderablesList = new LinkedList<Renderable>();
		try {
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.setTitle("TileEngine");
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(1);
		}
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, 800, 0, 600, 1, -1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		
		System.out.print("Initilized RenderManager.\n");
	}
	public void registerRenderables(Renderable target){
		renderablesList.add(target);
	}
	@Override
	public Boolean update(long delta) {
		Display.update();
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		for(int i = 0; renderablesList.size() <= i; i++){
			renderablesList.get(i).draw();
		}
		return true;
	}
	public static RenderManager getInstance() {
		if(instance == null){
			instance = new RenderManager();
		}	
		return instance;
	}
}
